package com.yangsir.mysite.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangsir.mysite.Dto.ArticleUserDto;
import com.yangsir.mysite.common.R;
import com.yangsir.mysite.mapper.ArticleMapper;
import com.yangsir.mysite.pojo.Article;
import com.yangsir.mysite.service.ArticleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * java 8
 */
@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleMapper articleMapper;

    /**
     * 保存前端传来的文章 (作者及管理员)
     */
    @PostMapping("/save")
    public R<String> saveArticle(@RequestBody Article article) {
        System.out.println(article.toString());
        if (article.equals(null)) {
            return R.error("信息不能为空");
        }
        return articleService.saveArticle(article);
    }

    /**
     * 根据文章id查询文章
     */
    @GetMapping("/getOneArticle")
    public R<Article> getOneArticle(Long id) {
        if (id == null) {
            return R.error("参数不能为空");
        }
        return articleService.getOneArticle(id);
    }

    /**
     * 分页查询文章并返回
     */
    @GetMapping("/getArticles")
    public R<Page> getArticle(Integer page, Integer pageSize, Integer type, Long categoryId, String clickedtags) {
        if (page == null || pageSize == null || type == null) {
            return R.error("参数不能为空");
        }
        return articleService.pageQuery(page, pageSize, type, categoryId, clickedtags);
    }

    /**
     * 随机查询文章
     */
    @GetMapping("/randomArticles")
    public R<List<Article>> randomArticles(Integer count) {
        List<Article> articles = articleMapper.randomArticles(count);
        return R.success(articles, "查询成功");
    }

    /**
     * 根据用户获取文章(作者及管理员)
     */
    @GetMapping("/myArticles")
    public R<List<ArticleUserDto>> getArticlesByUserId(HttpServletRequest httpServletRequest) {
        return articleService.getArticlesByUserId(httpServletRequest);
    }

    /**
     * 查询文章(作者及管理员)
     */
    @GetMapping("/getArticlesByStr")
    public R<List<ArticleUserDto>> getArticlesByStr(String str) {
        if (str == null) {
            return R.error("参数不能为空");
        }
        return articleService.getArticlesByStr(str);
    }

    /**
     * 删除文章(作者及管理员)
     */
    @DeleteMapping("/deleteArticle")
    public R<String> deleteArticle(Long id) {
        if (id == null) {
            return R.error("参数不能为空");
        }
        return articleService.deleteArticle(id);
    }

    /**
     * 获取置顶文章
     */
    @GetMapping("/getTopArticles")
    public R<List<Article>> getTopArticle() {
        List<Article> articles = articleService.getTopArticle();
        return R.success(articles, "查询成功");
    }

    /**
     * 设置文章置顶(管理员)
     */
    @PostMapping("/setTopArticles")
    public R<String> topArticle(@RequestBody Article article) {
        if (article.getId() == null) {
            return R.error("参数不能为空");
        }
        return articleService.topArticle(article);
    }

    /**
     * 根据分类获取文章(作者及管理员)
     */
    @GetMapping("/getArticlesByTypeOrCategory")
    public R<List<ArticleUserDto>> getArticlesByTypeOrCategory(Integer type, Long categoryId, HttpServletRequest httpServletRequest) {
        if (type == null) {
            return R.error("参数不能为空");
        }
        return articleService.getArticlesByTypeOrCategory(type, categoryId, httpServletRequest);
    }

    /**
     * 根据分类获取文章
     * @param categoryId
     * @return
     */
    @GetMapping("/getArticlesByCategory")
    public R<List<Article>> getArticlesByCategory(Long categoryId) {
        if (categoryId == null) {
            return R.error("参数不能为空");
        }
        return articleService.getArticlesByCategory((categoryId));
    }
}
